﻿using CefSharp;
using CefSharp.WinForms;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Edu.ZJ
{
    public class ExtChromiumBrowser : ChromiumWebBrowser
    {
        public ExtChromiumBrowser(string url) : base(url)
        {
            
        }

        public event EventHandler<NewWindowEventArgs> StartNewWindow;

        public void OnNewWindow(NewWindowEventArgs e)
        {
            StartNewWindow?.Invoke(this, e);
        }

        public void ExecuteJs(string js)
        {
            GetBrowser().MainFrame.ExecuteJavaScriptAsync(js);
        }

        public async Task<JavascriptResponse> EvaluateScriptAsync(string script, string scriptUrl = "about:blank", int startLine = 1, TimeSpan? timeout = null, bool useImmediatelyInvokedFuncExpression = false)
        {
            return await GetBrowser().MainFrame.EvaluateScriptAsync(script, scriptUrl, startLine, timeout, useImmediatelyInvokedFuncExpression);
        }

        public void BtnClickById(string id)
        {
            string js = $"document.getElementById('{id}').click();";
            ExecuteJs(js);
        }

        public void SetInputValue(string id, string value)
        {
            string js = $"document.getElementById('{id}').value='{value}';";
            ExecuteJs(js);
        }
    }
}
